package com.aldobucchi.mouse
{
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	import com.aldobucchi.utils.App;
	import com.aldobucchi.x.flash.events.EventListenerSpec;


	public class MouseDragPanner extends MouseTrackerBase
	{



		private var absDiff:Point;

		
		private var dragLog:Array;
		
		
		private var inertia:MouseDragInertia;


		
		/////////////////////////////////////
		// dragging
		/////////////////////////////////////

		private var _dragging:Boolean;
		[Bindable("draggingChanged")]
		public function get dragging( ):Boolean
		{
			return _dragging;
		}

		private function setDragging( v:Boolean ):void
		{
			if ( v == _dragging )
				return;
			_dragging = v;
			
			
			if ( v )
			{
				
				if ( inertia && inertia.isPlaying )
					inertia.stop( );
					
				inertia = new MouseDragInertia( target );
				
				try {
					App.app.addEventListener( Event.ENTER_FRAME, app_enterFrame	, false, 0, true );
					App.app.addEventListener( MouseEvent.MOUSE_UP, app_mouseUp	, false, 0, true );
				}catch(e:Error){}
			}
			else
			{
				try {
					App.app.removeEventListener( Event.ENTER_FRAME, app_enterFrame );
					App.app.removeEventListener( MouseEvent.MOUSE_UP, app_mouseUp );
				}catch(e:Error){}
			}
			
			
			
			dispatchEvent( new Event( "draggingChanged" ) );
		}

		/////////////////////////////////////
		// etc...
		/////////////////////////////////////



		public function MouseDragPanner( )
		{
		}
		
		
		
		override protected function getTargetEventListenerSpecs( ):Array
		{
		
			var els:Array = super.getTargetEventListenerSpecs();
			els.push( new EventListenerSpec( MouseEvent.MOUSE_DOWN,	target_mouseDown, 	false, 0, true ) );
			return els;
		}
		
		

		
		

		private function target_mouseDown( event:MouseEvent ):void
		{


			if ( ! target )
				return;
				
				
			if ( target.hitTestPoint( event.stageX, event.stageY, false ) )
			{
				
				setDragging( true );			

				var loc:Point = new Point( target.x, target.y );

				absDiff = target.parent.globalToLocal( MouseUtil.getMousePointOnStage( ) );
				absDiff = absDiff.subtract( loc );
			
				inertia.logPoint( loc );
			
			}
			
		}		

				

		private function app_enterFrame( event:Event ):void
		{

			if ( !dragging )
				return;
				
			try {
			
				var p:Point = target.parent.globalToLocal( MouseUtil.getMousePointOnStage( ) );
				p = p.subtract( absDiff );
				
				target.x = p.x;
				target.y = p.y;
				
				inertia.logPoint( p );
			
			} catch ( e:Error ){}
			
		}		


		private function app_mouseUp( event:MouseEvent ):void
		{


			if ( dragging )
			{
				setDragging( false );
				inertia.start( );
			}
		
		}
		
		
		
	}
	
}




/*
 
		override protected function dependenciesUpdated( ):void
		{
			
			super.dependenciesUpdated( );
			
			if ( dh.modifications[ targetSpec ] ) // update parent event listeners
			{
				var mods:Array = dh.modifications[ targetSpec ] as Array;
				try {
					( mods[ 0 ] as DisplayObject )
							.removeEventListener( MouseEvent.MOUSE_MOVE, target_parent_mouseMove );
				}catch ( e:Error ){}		
				try {
					( mods[ 1 ] as DisplayObject )
							.addEventListener( MouseEvent.MOUSE_MOVE, target_parent_mouseMove, false, 0, true );
				}catch ( e:Error ){}		
			}		
		
		} 
 
 */